package com.example.labtest.service.impl;

import com.example.labtest.dao.StudentInfoRepository;
import com.example.labtest.dao.StudentRepository;
import com.example.labtest.domain.Student;
import com.example.labtest.domain.Student_info;
import com.example.labtest.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Service
public class StudentInfoImplService implements StudentInfoService {

    @Autowired
    StudentInfoRepository studentInfoRepository;

    /**
     * 读取所有的学生数据
     * @return
     */
    public List<Student_info> findAll()
    {
        return studentInfoRepository.findAll();
    }

    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Student_info> findAll(Pageable pageable){

        return studentInfoRepository.findAll(pageable);
    };

    /**
     * 分页，带过滤的查询,按照Student来过滤
     */
    public Page<Student_info>findAll(Example<Student_info> student_info, Pageable pageable)
    {
        return studentInfoRepository.findAll(student_info,pageable);
    }

    /**
     * 分页按名字查找学生信息

     */
    public Page<Student_info>findBySName(String sname,Pageable pageable){
        return studentInfoRepository.findBySname(sname,pageable);
    }


    //实现接口

    public List<Student_info>findByNameAndPsw(String sname,String psw){
        return studentInfoRepository.findBySnameAndPsw(sname,psw);

    }

    /**
     * 插入学生
     * @param student
     * @return
     */
    /**
     *
     * @Transactional可以说是spring中最常用的注解之一了，
     * 通常情况下我们在需要对一个service方法添加事务时，加上这个注解，如果发生unchecked
     *exception，就会发生rollback，最典型的例子如下。
     */
    @Transactional
    public Student_info insert(Student_info student_info)
    {
        return studentInfoRepository.save(student_info);
    }

    /**
     * 更新学生
     * @param student_info
     * @return
     */
    @Transactional
    public Student_info update(Student_info student_info)
    {
        return studentInfoRepository.save(student_info);
    }

    /**
     * 删除学生
     * @param id
     */
    @Transactional
    public void delete1(Long id){studentInfoRepository.deleteById(id);}


    /**
     * 删除学生
     */

    public void delete2(Student_info student_info)
    {
        studentInfoRepository.delete( student_info);
    }

    public void delete(Long id){
        Student_info student_info=new Student_info();
         student_info.setId(id);
        studentInfoRepository.delete( student_info);
    }

    /**
     * 查找学生
     * @param id
     * @return
     */
    @Transactional
    public Student_info  search(Long id)
    {return studentInfoRepository.findById(id).get();}

    @Transactional
    public Student_info  getById(Long id)
    {
        Student_info student_info=studentInfoRepository.findById(id).orElse(null);
        return  student_info;
    }

    /**
     * 按名字查询学生信息
     * @param name
     * @return
     */
    @Transactional
    public List<Student_info > findByName(String name){

        return studentInfoRepository.findBySname(name);

    }

    public Page<Student_info >findByNameLike(String name,Pageable pageable){
        return studentInfoRepository.findBySnameLike(name,pageable);
    }

}
